nUsers = 1000990;
nItems = 624961;
nRatings = 262810175;
nTrainRatings = 252800275;
nProbeRatings = 4003960;
nTestRatings = 6005940;

runmode=1;     % 1 - train, 2 - validation, 3 - test

filename = '';
ratings = 0;

switch runmode
    case 1,
        filename = '/home/prashant/Workspace/CF/KDDCup/track1/trainIdx1.txt';
        matfile = 'kddTrain.mat';
        %filename = '/home/prashant/Workspace/CF/KDDCup/sample_track1/trainIdx1.firstLines.txt';
        %matfile = 'kddSampleTrain.mat';
        ratings = nTrainRatings;
    case 2,
        filename = '/home/prashant/Workspace/CF/KDDCup/track1/validationIdx1.txt';
        matfile = 'kddValidate.mat';
        %filename = '/home/prashant/Workspace/CF/KDDCup/sample_track1/validationIdx1.firstLines.txt';
        %matfile = 'kddSampleValidate.mat';
        ratings = nProbeRatings;
    case 3,
        filename = '/home/prashant/Workspace/CF/KDDCup/track1/testIdx1.txt';
        matfile = 'kddTest.mat';
        ratings = nTestRatings;
end

userIdx = zeros(ratings,1);
itemIdx = userIdx;
dateIdx = userIdx;
score = userIdx;

fid = fopen(filename, 'r');

iter = 1;
try    
    for u = 1:nUsers
        [a,c] = fscanf(fid,'%d|%d',2);
        assert(c==2 ,'failed at 43');
    
        for i= 1:a(2)
            [b,c] = fscanf(fid,'%d %d %d %d:%d:%d',6);
            assert(c==6, 'failed at 47');
        
            userIdx(iter) = a(1) + 1;
            itemIdx(iter) = b(1) + 1;
            score(iter) = b(2);
            dateIdx(iter) = b(3);
            
            iter = iter + 1;
        end
    end
catch ex
    disp(ex.message);
end
   
data.triplet = [userIdx itemIdx dateIdx];
data.score = score;
data.size = [nUsers nItems max(dateIdx)];

save(matfile, 'data');